//
//  ExaminaWaitExamineController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/19.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "ExaminaWaitExamineController.h"
#import "ExaminaColumnFirstCell.h"
#import "ExaminaColumnReasonView.h"
#import "ExaminaColumnHeaderWithSectionView.h"

@interface ExaminaWaitExamineController ()<UITableViewDelegate,UITableViewDataSource,ExaminaColumnReasonViewDelegate,ExaminaColumnFirstCellDelegate>
@property (nonatomic,strong) NSMutableArray *subjectTwoArr;
@property (nonatomic,strong) NSMutableArray *subjectThreeArr;
@property (nonatomic,strong) NSMutableArray *tempSubjectTwoArr;
@property (nonatomic,strong) NSMutableArray *tempSubjectThreeArr;
@property (nonatomic,strong) NSMutableDictionary *resonDic;
@property (nonatomic,strong) ExaminaColumnReasonView *reasonView;
@property (nonatomic,strong) ExaminaColumnHeaderWithSectionView *hederWithOneSectionView;
@property (nonatomic,strong) ExaminaColumnHeaderWithSectionView *hederWithTwoSectionView;
@end

@implementation ExaminaWaitExamineController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
    [self loadData];
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    if (self.state==3) {
        [self setNavBack];
        self.title = @"待考试";
    }
    [self.view addSubview:self.tableView];
}

#pragma mark ==========加载数据==========
- (void)loadData{
    [self showLoading];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:[NSNumber numberWithInteger:self.state] forKey:@"state"];
    [dic setValue:[NSNumber numberWithInteger:self.state==3?3:0] forKey:@"applicationStatus"];
    [[NetworkEngine sharedClient] GETByToken:EXAMINAMANAGEMENEXAMINEDATEList data:dic success:^(id responseObject) {
        [self hideHUD];
        if ([[responseObject objectForKey:@"code"] integerValue]==1) {
            self.subjectTwoArr = [[NSMutableArray alloc] initWithArray:[TeachManagementModel mj_objectArrayWithKeyValuesArray:[[[responseObject objectForKey:@"data"] objectAtIndex:0] objectForKey:@"data"]]];
            self.subjectThreeArr = [[NSMutableArray alloc] initWithArray:[TeachManagementModel mj_objectArrayWithKeyValuesArray:[[[responseObject objectForKey:@"data"] objectAtIndex:1] objectForKey:@"data"]]];
            [self.tableView reloadData];
        } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
            [self toLoginVC];
        } else {[self showMessage:[responseObject objectForKey:@"msg"]];}
    } failure:^(NSString *error) {
        [self showError:error];
    }];
}

#pragma mark ==========懒加载==========
- (ExaminaColumnReasonView *)reasonView{
    if (!_reasonView) {
        _reasonView = [[ExaminaColumnReasonView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _reasonView.delegate = self;
    }
    return _reasonView;
}
- (ExaminaColumnHeaderWithSectionView *)hederWithOneSectionView{
    if (!_hederWithOneSectionView) {
        _hederWithOneSectionView = [[ExaminaColumnHeaderWithSectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110*SCALEUNIT) WithType:1 WithTitle:@"科目二" WithSection:0];
        [_hederWithOneSectionView.actionBtn addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hederWithOneSectionView;
}
- (ExaminaColumnHeaderWithSectionView *)hederWithTwoSectionView{
    if (!_hederWithTwoSectionView) {
        _hederWithTwoSectionView = [[ExaminaColumnHeaderWithSectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110*SCALEUNIT) WithType:1 WithTitle:@"科目三" WithSection:1];
        [_hederWithTwoSectionView.actionBtn addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hederWithTwoSectionView;
}
- (UITableView *)tableView{
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 30*SCALEUNIT, 0, 30*SCALEUNIT)];
        [_tableView setSeparatorColor:RGBA(230, 230, 230, 1)];
        [_tableView registerClass:[ExaminaColumnFirstCell class] forCellReuseIdentifier:@"ExaminaColumnFirstCell"];
    }
    return _tableView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section==0?self.subjectTwoArr.count:self.subjectThreeArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 155*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ExaminaColumnFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminaColumnFirstCell" forIndexPath:indexPath];
    if (self.state == 3) {
        cell.waitExaminationModel = [indexPath.section==0?self.subjectTwoArr:self.subjectThreeArr objectAtIndex:indexPath.row];
    } else if (self.state == 2) {
        cell.schoolModel = [indexPath.section==0?self.subjectTwoArr:self.subjectThreeArr objectAtIndex:indexPath.row];
    } else {
        cell.delegate = self;
        cell.model = [indexPath.section==0?self.subjectTwoArr:self.subjectThreeArr objectAtIndex:indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==0?110*SCALEUNIT:110*SCALEUNIT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return section==0?self.hederWithOneSectionView:self.hederWithTwoSectionView;
}

#pragma mark ==========ExaminaColumnHeaderWithSectionViewDelegate展开+收起==========
- (void)refreshView:(UIButton *)btn{//btn.selected==NO数据展开操作 YES数据收起操作
    NSInteger section = btn.tag-10000;
    if (!btn.selected) {//收起操作:删除
        if (section==0) {
            self.tempSubjectTwoArr = [NSMutableArray arrayWithArray:self.subjectTwoArr];
        } else {
            self.tempSubjectThreeArr = [NSMutableArray arrayWithArray:self.subjectThreeArr];
        }
        [section==0?self.subjectTwoArr:self.subjectThreeArr removeAllObjects];
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:section];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [btn setTitle:@"展开" forState:UIControlStateNormal];
    } else {//展开操作:添加
        if (section==0) {
            [self.subjectTwoArr addObjectsFromArray:self.tempSubjectTwoArr];
        } else {
            [self.subjectThreeArr addObjectsFromArray:self.tempSubjectThreeArr];
        }
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:section];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [btn setTitle:@"收起" forState:UIControlStateNormal];
    }
    btn.selected = !btn.selected;
}

#pragma mark ==========ExaminaColumnFirstCellDelegate(同意+拒绝)==========
- (void)examinaColumnFirstCellDelegateWithState:(NSInteger)state WithID:(long)idNum{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:[NSNumber numberWithInteger:state] forKey:@"caochCheckResult"];
    [dic setValue:[NSNumber numberWithLong:idNum] forKey:@"id"];
    if (state==1) {//同意
        [dic setValue:@"" forKey:@"msg"];
        [[NetworkEngine sharedClient] POSTByToken:EXAMINAMANAGEMENEXAMINEPASSORNOPASSWITHCOACH data:dic success:^(id responseObject) {
            if ([[responseObject objectForKey:@"code"] integerValue]==1) {
                self.refreshNum++;
                [self loadData];
            } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
                [self toLoginVC];
            } else {
                [self showMessage:[responseObject objectForKey:@"msg"]];
            }
        } failure:^(NSString *error) {
            [self showError:error];
        }];
    } else {//拒绝
        self.resonDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        [self.view addSubview:self.reasonView];
        [self.reasonView doAnimation];
    }
}

#pragma mark ==========ExaminaColumnReasonView(拒绝原因)==========
- (void)trainManageSelectViewDelegateWithReason:(NSString *)content WithState:(BOOL)stateJudge{
    [self.reasonView removeFromSuperview];
    if (!stateJudge) {
    } else {
        [self.resonDic setValue:content forKey:@"msg"];
        [[NetworkEngine sharedClient] POSTByToken:EXAMINAMANAGEMENEXAMINEPASSORNOPASSWITHCOACH data:self.resonDic success:^(id responseObject) {
            if ([[responseObject objectForKey:@"code"] integerValue]==1) {
                self.refreshNum++;
                [self loadData];
            } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
                [self toLoginVC];
            } else {
                [self showMessage:[responseObject objectForKey:@"msg"]];
            }
        } failure:^(NSString *error) {
            [self showError:error];
        }];
    }
}

- (void)dealloc{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
